//
//  main.m
//  P1111
//
//  Created by 闫潇 on 14/11/11.
//  Copyright (c) 2014年 yan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:5];
    //长度为5的可变数组
    NSLog(@"%ld",[mArray count]);//此时长度为0
    
    //声明几个学生对象，然后进行排序
    Student *stu1 = [Student new];
    Student *stu2 = [Student new];
    Student *stu3 = [Student new];
    
    [stu1 setName:@"孙悟空"];
    [stu1 setAge:1000];
    [stu2 setName:@"猪八戒"];
    [stu2 setAge:1001];
    [stu3 setName:@"沙和尚"];
    [stu3 setAge:999];
    
    [mArray addObject:stu1];
    [mArray addObject:stu2];
    [mArray addObject:stu3];
    NSLog(@"%ld",[mArray count]);//此时长度为3
    
    NSLog(@"排序前为：");
    for (int i = 0; i < [mArray count]; i++)
    {
        NSLog(@"%@",[mArray objectAtIndex:i]);
    }
    //定义一个排序描述符对象
    NSSortDescriptor *sd1 = [NSSortDescriptor
                             sortDescriptorWithKey:@"name" ascending:YES];
    //定义一个数组，然后用来盛放排序条件
    NSArray *descriptors = [NSArray arrayWithObject:sd1];
    //进行排序
    [mArray sortUsingDescriptors:descriptors];
    NSLog(@"排序后为：");
    for (int i = 0; i < [mArray count]; i++)
    {
        NSLog(@"%@",[mArray objectAtIndex:i]);
    }
    //对于NSArray 的排序
    NSArray *fetchJingTeam = [NSArray arrayWithObjects:@"stu1",@"stu2",@"stu3", nil];
    NSLog(@"-----\n 排序前为：");
    for (int i = 0; i < [fetchJingTeam count]; i++)
    {
        NSLog(@"%@",[fetchJingTeam objectAtIndex:i]);
    }
    /* 
     不可变数组排序之后需要使用新的数组进行接受排序结果，原始数组不变，而可变数组可以直接在原始的数组上进行排序
     */
//    [fetchJingTeam sortedArrayUsingDescriptors:descriptors];
    NSArray *fakeFetchJingTeam = [fetchJingTeam
                                  sortedArrayUsingDescriptors:descriptors];
    NSLog(@"排序后原队伍为：");
    for (int i = 0; i < [fetchJingTeam count]; i++)
    {
        NSLog(@"%@",[fetchJingTeam objectAtIndex:i]);
    }
    
    return 0;
}
